﻿
@{
    ViewBag.Title = "ChargeIndex";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control form-control-focus" placeholder="请输入卡号" style="width: 200px;">
                    </div>
                </td>
                <td style="padding-left:12px;">
                    <div class="btn-group" id="btnKeywordTypeGourp" style="margin-top:0;">
                        <button type="button" data-keywordtype="kh" class="btn active btn-restore">&nbsp;卡&nbsp;&nbsp;号&nbsp;</button>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<form name="form1" id="formPat1">
    <div class="panel panel-default" style="margin-top:10px;">
        <div class="panel-heading navb-bg">
            收费
        </div>
        <div style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="required">*</span>卡号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="kh" name="kh" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>门诊号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="mzh" name="mzh" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>病历号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="blh" name="blh" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>发票号：</th>
                    <td class="formValue">
                        <input type="text" id="txtfph" class="form-control  formClearIgnore newtouch_Readonly" style="width: 50%;display: inline;" />
                        <input type="button" class="btn btn-default btn-md" value="发票号 " onclick="ShowInvoicePanel()" />
                    </td>
                </tr>
            </table>
        </div>
        <hr />
        <div style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="required">*</span>姓名：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="xm" name="xm" />
                    </td>
                    <th class="formTitle">性别：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="xb" name="xb" />
                    </td>
                    <th class="formTitle">出生日期：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="csny" name="csny" />
                    </td>
                    <th class="formTitle">初复诊：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="fzbz" name="fzbz" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle"><span class="required">*</span>病人性质：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="brxzmc" name="brxzmc" />
                    </td>
                </tr>
            </table>
        </div>
        <hr />
        <div style="padding: 2px;padding-right:20px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="required">*</span>挂号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="mzgh" name="mzgh" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>医生：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="ys" name="ys" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>科室：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="ks" name="ks" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>单价：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="dj" name="dj" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle"><span class="required">*</span>收费项目：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="sfxm" name="sfxm" placeholder="请输入精确查询条件" />
                    </td>
                    <th class="formTitle"><span class="required">*</span>数量：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="sl" name="sl" />
                    </td>
                    <th class="formTitle">组号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control formClearIgnore" id="czh" name="czh" />
                    </td>
                    <th class="formTitle">单位：</th>
                    <td class="formValue">
                        <input type="text" class="form-control newtouch_Readonly" id="dw" name="dw" />
                    </td>

                </tr>
            </table>
        </div>
    </div>

</form>
<form id="tabGrid">
    <div>
        <div class="gridPanel">
            <table id="listGrid" rel="tabGrid"></table>
        </div>
    </div>
</form>
<div class="panel-heading" id="panelZje">
    <div style="margin-left:40%;">
        <table>
            <tbody>
                <tr>
                    <th class="formTitle">
                        当前处方号：
                    </th>
                    <td class="formValue"><label id="lbldqcfh" style="padding-right:20px;"></label></td>
                    <th class="formTitle">
                        当前处方金额：
                    </th>
                    <td class="formValue"><label id="lbldqxmje">0.00</label>元</td>
                    <th class="formTitle">
                        处方总金额：
                    </th>
                    <td class="formValue"><label id="lblzje">0.00</label>元</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<!--门诊收费-->
@Html.Partial("ChargeAddPay")
<div style="clear:both;"></div>
<!--按钮工具条-->
<div class="toolbar toolbarfloat">
    <a class="btn btn-primary" onclick="newtouch_globalevent_f4()"><i class="fa fa-close"></i> F4:清除</a>
    <a class="btn btn-primary" onclick="ChangeCF()"><i class="fa fa-edit"></i>换方</a>
    <a class="btn btn-primary" onclick="newtouch_globalevent_f8()"><i class="fa fa-save"></i> F8:保存</a>
    <a class="btn btn-primary" onclick="ItemPlate()"><i class="fa "></i>模版</a>
    <a class="btn btn-primary" onclick="DeleteGrid()"><i class="fa fa-delete"></i>删除</a>
</div>

<script>
    $(function () {
        localStorage.clear();
        loadPage();
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            initLayout("tabGrid");
        });
        var txtKeyword = document.getElementById('txt_keyword');
        txtKeyword.addEventListener('keydown', function (event) {
            event = event || window.event;
            if (event.keyCode === 13) {
                var keyword = $.trim($("#txt_keyword").val());
                searchPatInfo(keyword);
            }
        });

        //卡号回车
        $("#kh").keyup(function (event) {
            var keyword = $.trim($("#kh").val());
            if (event.keyCode === 13) {
                if (keyword == null) {
                    $.modalAlert("请输入有效卡号", 'warning');
                    return false;
                } else {
                    searchPatInfo(keyword);
                }
            }
        });

        //收费项目bind浮层
        loadYpItemData();

        //挂号回车
        bindGHSource(mzghData);

        //医生bing浮层
        bindDoctors();

        GetNewCFH(); //获取新处方号

        //科室回车
        $("#ks").keyup(function (event) {
            var ks = $("#ks").val();
            if (event.keyCode === 13) {
                if (ks == null) {
                    $.modalAlert("科室不能为空", 'warning');
                    return false;
                } else {
                    //添加录入信息的判断
                    AddData_Check();
                }
            }
        });

        //组号回车
        $("#czh").keyup(function (event) {
            if ($(this).hasClass('form-an-cur')) {
                return; //刚从上一个节点自动跳过来的
            }
            if (event.keyCode == 13) {
                //添加录入信息的判断
                AddData_Check();
            }
        });

        //支付金额回车
        $("#txtmoney").keyup(function (event) {
            if (event.keyCode == 13) {
                zfje = $("#txtmoney").val();  //支付金额
                ysk = $("#txtysk").val(); //应收款
                zl = parseFloat(zfje) - parseFloat(ysk);
                zl = zl.toFixed(2);
                $("#txtzl").val(zl);
            }
        });

        //全选
        $("#tabGrid input[type=checkbox]:eq(0)").bind("click", function () {
            grid_checkedit();
            if ($(this)[0].checked == true) {
                //选中
            }
        });

    });

    //初始化页面txtysk
    function loadPage() {
        GetInvoiceListByEmpCode(); //获取发票号
        $('#listGrid').jqGrid("clearGridData");//先清空数据
        gridListData([]); //加载jqGrid

        //成组号
        $("#czh").val(0);
        //获取日期
        $("#txtzfrq").val($.getDate);
        //金额初始化为0
        $("#lbldqxmje,#lblzje").text("0.00");
        $("#lbldqcfh").text("");
        $("#txtysk,#txtzl").val("0.00");
        $("#mzgh,#ys").removeAttr("disabled").css("background-color", "white");
        myCFData = [];
        myCFCount = 0;
        localStorage.removeItem("brxzData");
    }

    var mzghData = [], //门诊挂号数据
        mzghDocVo = null, //门诊挂号-专家科室
        myCFData = [],    //处方列表数据集合
        myCFCount = 0,   //处方列表行数
        iszhbrxz = false, //判断病人性质是否组合病人性质
        zh_sbrxz = "", //首、末病人性质
        zh_sbrxzmc = "",
        zh_mbrxz = "",
        zh_mbrxzmc = "",
        zh_sbrxzbh = 0,
        zh_mbrxzbh = 0,
        lsCfh = "",     //临时处方号
        sfxmbh = "",   //收费项目int型编号
        sfxmcbh = "",   //收费项目char型编号
        sfxmzfbl = "",  //收费项目自负比例
        sfxmdlmc = "",  //收费项目大类名称
        sfxmdl = "",    //收费项目大类
        sfxmzfxz = "",  //收费项目自负性质
        sfxmtype = "",  //收费项目类别（例如：挂号项目、药品处方类项目、检验、检查）
        sfxmypgg = "", //药品规格
        sfxmNbdl = "", //农保大类
        sfxmYbdm = "", //医保代码
        sfxmkeyword = "", //存取收费项目输入条件
        //ysksbh = "", //医生科室编号
        mzgh = "", //挂号
        ks = "", //科室CODE
        ysbh = "", //医生编号
        ys = "", //医生CODE
        lsysbh = "", //当挂号为专家号时使用
        ghnm = "", //挂号内码
        brxz = "", //病人性质
        patInfoObj = {}, //个人信息Object对象
        mzYbjylx = 0,//医保交易类型
        mjzbz = "", //门急诊标识
        stateSave = true,//判断数据是否变更
        isYbbr = false,//是否医保病人
        isNdzdbxm = false,//是否尿毒症大病项目
        isPtdbxz = false,//是否普通大病
        lsCzh = "",//成组号
        yfCode = "",//药房代码
        ypbzdm = "",//包装代码
        sfxmfwf = "", //收费项目服务费
        cffwf = new Object(),  //处方收费项目服务费后台返回数据
        jeNum = 0, //当前项目金额 （包括服务费）
        dqzje = 0, //当前总金额
        cfzje = 0, //处方总金额
        dqcfhList = [], //存放当前处方号
        messCFXM = "", //相同药品的错误信息
        kh = "";//卡号

    //获取发票号
    function GetInvoiceListByEmpCode() {
        $.najax({
            url: "/OutpatientManage/OutpatientReg/GetInvoice?r=" + Math.random(),  //注意
            dataType: "text",
            async: false,
            success: function (data) {
                $("#txtfph").val(data);
                fph = data;
            }
        });
    }

    //弹出发票号的窗口
    function ShowInvoicePanel() {
        $.modalOpen({
            id: "InvoiceNoPanel",
            title: "选发票号",
            url: "/OutpatientManage/OutpatientReg/ChooseInvoice?r=" + Math.random(),
            width: "300px",
            height: "200px",
            callBack: function (iframeId) {
                top.frames[iframeId].checkFPH();//窗口点确定的回调函数
            }
        });
    }

    //根据卡号获取病人基本信息
    function searchPatInfo(keyword) {
        if (keyword === '') {
            return false;
        }
        kh = keyword;
        $.najax({
            type: "POST",
            url: "/OutpatientManage/OutpatCharge/ChargeIndex",
            data: { kh: keyword, brxz: "" },
            dataType: "json",
            success: function (ajaxresp) {
                funcDoPageClear();//清除
                var mzghbrxzData = ajaxresp.data.patInfo.brxzList;
                if (mzghbrxzData && mzghbrxzData.length && mzghbrxzData.length > 1) {
                    //弹出 多个病人性质的窗口
                    bindBrxz();
                }
                else if (mzghbrxzData == null)//只有一个病人性质
                {
                    setPatInfoModel(ajaxresp.data.patInfo); //个人基本信息赋值
                    mzghData = ajaxresp.data.ghItemList; //绑定挂号信息
                    mzghDocVo = ajaxresp.data.ghDocVo; //专家信息
                }
            },
            errorCallback: function (ajaxresp) {
                window.newtouch_globalevent_f4(); //清除
            }
        });
    };

    //清除操作
    var funcDoPageClear = function () {
        loadPage();
    };

    //初始化病人信息
    function setPatInfoModel(patModel) {
        $('#formPat1').formSerialize(patModel);
        $("#xb").val($.getGender(patModel.xb));
        $("#csny").val((patModel.csny && patModel.csny.length >= 10 ? patModel.csny.substring(0, 10) : ""));
        $("#fzbz").val(patModel.fzbz == "1" ? "复诊" : "首诊");
        brxz = patModel.brxz;
        //加载病人信息
        patInfoObj.kh = patModel.kh;  //病人卡号
        patInfoObj.xm = patModel.xm; //姓名
        patInfoObj.xb = patModel.xb; //性别
        patInfoObj.csny = patModel.csny;
        patInfoObj.patid = patModel.patid; //病人内码
        patInfoObj.brxz = patModel.brxz;  //病人性质
        patInfoObj.brxzbh = patModel.brxzbh; //病人性质编号
        patInfoObj.brxzmc = $("#brxzmc").val();
        patInfoObj.blh = patModel.blh; //病历号
        patInfoObj.zjh = patModel.zjh; //证件号
        patInfoObj.zjlx = patModel.zjlx; //证件类型
    }

    //加载病人性质
    function bindBrxz() {
        localStorage.setItem("brxzData", JSON.stringify(mzghbrxzData));
        $.modalOpen({
            id: "BrxzForm",
            title: "病人性质窗口",
            url: "/OutpatientManage/OutpatCharge/ChargeBrxzDialog?r=" + Math.random(),
            width: "300px",
            height: "350px",
            callBack: function (iframeId) {

            }
        });
    }

    //后台根据病人性质和卡号ajax查询人员数据
    function getChargeInfoAjax(patobj) {
        var cardNo = $("#txt_keyword").val();
        $.najax({
            type: "POST",
            url: "/OutpatientManage/OutpatCharge/ChargeIndex?r=" + Math.random(),
            data: { kh: cardNo, brxz: patobj.brxz },
            dataType: "json",
            success: function (ajaxresp) {
                funcDoPageClear();//清除
                setPatInfoModel(ajaxresp.data.patInfo); //个人基本信息赋值
                mzghData = ajaxresp.data.ghItemList; //绑定挂号信息
                mzghDocVo = ajaxresp.data.ghDocVo; //专家信息
            },
            errorCallback: function (err) {
                $.loading(false);
                newtouch_globalevent_f4(); //清除
            },
            complete: function () {
                $.loading(false);
            }
        });
    }


    //加载挂号数据
    function bindGHSource() {
        $("#mzgh").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            height: 200,
            width: 600,
            filter: function () {
                return mzghData;
            },
            caption: "选择挂号",
            colModel: [
                { label: '资源', name: 'gh_ks', widthratio: 25 },
                { label: 'ghnm', name: 'ghnm', widthratio: 25, hidden: true },
                { label: 'ks', name: 'ks', widthratio: 25, hidden: true },
                { label: 'ts', name: 'ys', widthratio: 25, hidden: true },
                { label: 'mjzbz', name: 'mjzbz', widthratio: 25, hidden: true },
                { label: 'dbxm', name: 'dbxm', widthratio: 25, hidden: true },
                { label: 'fzbz', name: 'fzbz', widthratio: 25, hidden: true },
                { label: '挂号', name: 'ghName', widthratio: 25 },
                { label: '科室', name: 'ksName', widthratio: 15 },
                { label: '挂号日期', name: 'CreateTime', widthratio: 35 }
            ],
            itemdbclickhandler: function ($thistr) {
                //赋值
                $("#mzgh").val($thistr.attr("data-ghName"));
                $("#ks").val($thistr.attr("data-ksName"));
                $("#fzbz").val($thistr.attr("data-fzbz") === "1" ? "复诊" : "首诊");

                ghnm = $thistr.attr("data-ghnm");
                //ysksbh = $thistr.attr("data-ksbh");
                mzgh = $thistr.attr("data-ghName");
                ks = $thistr.attr("data-ks");
                mjzbz = $thistr.attr("data-mjzbz");
                var lsys = $thistr.attr("data-ys");

                $.each(top.window.clients.doctorInHosBookkeep, function (idx, val) {
                    if (val.ks === ks && val.StaffGh === lsys) {
                        ys = val.StaffGh;
                        $("#ys").val(val.StaffName);
                    }
                });
                if (mzghDocVo != null && mzghDocVo.ry) {
                    $("#ys").val(mzghDocVo.rymc);
                    ys = mzghDocVo.gh;
                    if (mjzbz !== "2") {
                        //ysksbh = mzghDocVo.ksbh;
                        ks = mzghDocVo.ks;
                    }
                }
            }
        });
    }

    //加载医生信息
    function bindDoctors() {
        ///医生
        $("#ys").newtouchFloatingSelector({
            height: 300,
            width: 500,
            filter: function (keyword) {
                if (ghnm == "") {
                    $modelAlert("挂号不能为空", "warning");
                    return false;
                }
                if (!keyword) {
                    return false;
                }
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();//
                $.each(top.window.clients.doctorInHosBookkeep, function (idx, val) {
                    if ($.trim(keyword).length == 0 || (val.py && val.py.toUpperCase().indexOf(keyword.toUpperCase()) >= 0) || (val.rymc && val.rymc.indexOf(keyword.toLowerCase()) >= 0) || (val.gh && val.gh.indexOf(keyword.toLowerCase()) >= 0) || (val.kspy && val.kspy.indexOf(keyword.toUpperCase()) >= 0)) {
                        if (ghnm && val.ks == ks) {
                            resultObjArr.push(val);
                        }
                    }
                });
                return resultObjArr;

            },
            caption: "医生科室对照",
            colModel: [
                { label: '人员名称', name: 'rymc', widthratio: 25 },
                { label: 'rygh', name: 'rygh', widthratio: 25, hidden: true },
                { label: 'ry', name: 'ry', widthratio: 25, hidden: true },
                { label: '科室名称', name: 'ksmc', widthratio: 25 },
                //{ label: 'ksbh', name: 'ksbh', widthratio: 25, hidden: true },
                { label: 'ks', name: 'ks', widthratio: 25, hidden: true }

            ],
            itemdbclickhandler: function ($thistr) {
                $("#ys").val($thistr.attr("data-rymc"));
                $("#ks").val($thistr.attr("data-ksmc"));
                ys = $thistr.attr("data-rygh");
                if (mjzbz != "2") {
                    //ysksbh = $thistr.attr("data-ksbh");
                    ks = $thistr.attr("data-ks");
                }
            },
        });

    }



    //录入信息判断
    function AddData_Check() {
        kh = $("#kh").val();
        if (kh == null || kh == undefined) {
            $.modalAlert("请选择挂号类型", 'warning');
            $("#kh").focus();
            return false;
        }
        else {
            var txtczh = $("#czh").val();
            //if (lsCzh != "") {
            if (txtczh == "") {
                $.modalAlert("请设置成组号", 'warning');
                $("#czh").focus();
                return false;
            }
            //} else {
            //$("#czh").val(0);
            //}
        }
        var sfxm = $("#sfxm").val();
        if (sfxm == null || sfxm === "" || sfxm == undefined) {
            $.modalAlert("收费项目不能为空", 'warning');
            $("#sfxm").focus();
            return false;
        }
        var sl = $("#sl").val();
        if (sl == null || sl == "" || sl == undefined) {
            $.modalAlert("数量不能为空", 'warning');
            $("#sl").focus();
            return false;
        }
        var dj = $("#dj").val();
        if (dj == null || dj === "" || dj == undefined) {
            $.modalAlert("单价不能为空，请录入正确的收费项目！", 'warning');
            $("#sfxm").focus();
            return false;
        }
        mzgh = $("#mzgh").val();
        if (mzgh == null || mzgh === "" || mzgh == undefined) {
            $.modalAlert("请选择挂号项目", 'warning');
            $("#mzgh").focus();
            return false;
        }
        var ys = $("#ys").val();
        if (ys == null || ys === "" || ys == undefined) {
            $.modalAlert("请选择医生", 'warning');
            $("#ys").focus();
            return false;
        }
        var ks = $("#ks").val();
        if (ks == null || ks == "" || ks == undefined) {
            $.modalAlert("请选择挂号项目", 'warning');
            $("#ks").focus();
            return false;
        }
        loadCFHAndInfo(); //后台调取处方号和服务费
    }

    //添加处方数据
    function AddCFData() {
        var obj = new Object();
        var messCfxm = "";
        if (sfxmtype === "1")//项目
        {
            obj.cfh = "";
        }
        if (sfxmtype == "2")//药品
        {
            obj.cfh = lsCfh;
            //同一处方不能录入相同的药品
            messCfxm = cheYPEqual(sfxmcbh, yfCode);
        }

        obj.xmmc = $("#sfxm").val(); //项目
        obj.dj = $("#dj").val();
        obj.dw = $("#dw").val();
        obj.sl = $("#sl").val();
        obj.sfxm = sfxmcbh;
        obj.sfxmdlmc = sfxmdlmc;
        obj.xmbh = sfxmbh;
        obj.sfxmzfbl = parseFloat(sfxmzfbl);
        obj.sfxmypgg = sfxmypgg;
        obj.je = jeNum;
        obj.ksmc = $("#ks").val();
        obj.ysmc = $("#ys").val();
        obj.xmnm = 0;
        obj.ghnm = ghnm;
        obj.mjzbz = mjzbz;
        //obj.ksbh = ysksbh;
        obj.ks = ks;
        obj.ys = ys;
        obj.dl = sfxmdl;
        obj.zfxz = sfxmzfxz;
        obj.xmtype = sfxmtype;
        obj.pt_nm = 0;
        obj.pt_zdmxnm = 0;
        obj.czh = $("#czh").val(); //组号
        obj.yfdm = yfCode;
        obj.ypbzdm = ypbzdm;
        obj.fwfdj = sfxmfwf; //收费项目服务费
        obj.nbdl = sfxmNbdl;
        obj.ybdm = sfxmYbdm;
        //如果已经添加项目或药品  判断 当前编号是否已经存在
        if (myCFData) {
            for (var i = 0; i < myCFData.length; i++) {
                if (myCFData[i].sfxmbh === sfxmbh) {
                    $.modalAlert("当前药品已经存在，或当前项目已经存在", 'warning');
                    clearItem();
                    return false;
                }
            }
        }
        obj = $.extend(obj, patInfoObj); //把patInfoObj对象中的值 补充到 obj对象里面
        myCFData.push(obj);
        myCFCount++;
        $('#listGrid').jqGrid("clearGridData");//先清空数据
        gridListData(myCFData);
        jeNum = jeNum.toFixed(2);
        $("#lbldqxmje").text(jeNum);//当前项目金额
        if (dqzje !== 0) {
            dqzje = parseFloat(dqzje) + parseFloat(jeNum);
        } else {
            dqzje = parseFloat(jeNum);
        }

        dqzje = dqzje.toFixed(2);
        $("#txtysk").val(dqzje);

        if (sfxmtype === "2")//药品
        {
            if (cfzje !== 0) {
                cfzje = parseFloat(cfzje) + parseFloat(jeNum);
            } else {
                cfzje = parseFloat(jeNum);
            }
            cfzje = cfzje.toFixed(2);
        }
        $("#lblzje").text(cfzje); //总金额

        checkedAll();
        clearItemXM();
    }

    //判断是否相同药品录入同一处方
    function cheYPEqual(sfxmcbh, yfCode) {
        var res = "";
        var cflength = myCFData.length;
        if (cflength > 0) {
            for (var i = 0; i < cflength; i++) {
                var obj = myCFData[i];
                if (obj.cfh !== "") { //药品
                    if (lsCfh !== "" && lsCfh === obj.cfh) {
                        if (yfCode === obj.yfdm) {
                            if (sfxmcbh === obj.sfxmcbh) {
                                res = "当前处方已存在所选药品，确认再添加吗？";
                            }
                        }
                        else {
                            res = "error";
                        }
                    }
                }
                else {
                    if (sfxmcbh === obj.sfxmcbh) {
                        res = "当前已存在所选项目，确认再添加吗？";
                    }
                }
            }
        }
        return res;
    }

    //加载处方信息
    function gridListData(mydata) {
        var $gridList = $("#listGrid");
        $gridList.dataNewGrid({
            height: 150,
            autowidth: false,
            width: $("#tabGrid").width() - 18,
            caption: "处方列表",
            sortable: false,
            colModel: [
                { label: '序号', name: 'xh', hidden: true, key: true },
                { label: '处方号', name: 'cfh', width: 140, align: 'left' },
                { label: '类型', name: 'sfxmdlmc', width: 80, align: 'left' },
                { label: '项目名称', name: 'xmmc', width: 150, align: 'left' },
                { label: '规格', name: 'sfxmypgg', width: 150, align: 'left' },
                { label: '单位', name: 'dw', width: 60, align: 'left' },
                {
                    label: '单价', name: 'dj', width: 100, align: 'left'
                },
                {
                    label: '自负比例', name: 'sfxmzfbl', width: 60, align: 'left', formatter: function (cellvalue) {
                        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                    }
                },
                { label: '数量', name: 'sl', width: 60, align: 'left' },
                { label: '金额', name: 'je', width: 60, align: 'left' },
                { label: '病人性质', name: 'brxzmc', width: 100, align: 'left' },
                { label: 'brxz', name: 'brxz', hidden: true },
                { label: 'xm', name: 'xm', hidden: true },
                { label: 'xb', name: 'xb', hidden: true },
                { label: 'xb', name: 'xb', hidden: true },
                { label: 'csny', name: 'csny', hidden: true },
                { label: 'blh', name: 'blh', hidden: true },
                { label: 'zjh', name: 'zjh', hidden: true },
                { label: 'zjlx', name: 'zjlx', hidden: true },
                { label: 'brxz', name: 'brxz', widthratio: 25, hidden: true },
                { label: 'brxzbh', name: 'brxzbh', widthratio: 25, hidden: true },
                { label: 'ghnm', name: 'ghnm', widthratio: 25, hidden: true },
                { label: 'patid', name: 'patid', widthratio: 25, hidden: true },
                { label: 'sfxm', name: 'sfxm', widthratio: 25, hidden: true },
                { label: 'ksmc', name: 'ksmc', widthratio: 25, hidden: true },
                { label: 'ysmc', name: 'ysmc', widthratio: 25, hidden: true },
                { label: 'xmtype', name: 'xmtype', widthratio: 25, hidden: true },
                { label: 'xmbh', name: 'xmbh', widthratio: 25, hidden: true },
                { label: 'xmnm', name: 'xmnm', widthratio: 25, hidden: true },
                { label: 'mjzbz', name: 'mjzbz', widthratio: 25, hidden: true },
                //{ label: 'ksbh', name: 'ksbh', widthratio: 25, hidden: true },
                { label: 'ks', name: 'ks', widthratio: 25, hidden: true },
                { label: 'ys', name: 'ys', widthratio: 25, hidden: true },
                { label: 'dl', name: 'dl', widthratio: 25, hidden: true },
                { label: 'zfxz', name: 'zfxz', widthratio: 25, hidden: true },
                { label: 'pt_nm', name: 'pt_nm', widthratio: 25, hidden: true },
                { label: 'pt_zdmxnm', name: 'pt_zdmxnm', widthratio: 25, hidden: true },
                { label: 'pt_zdmxxz', name: 'pt_zdmxxz', widthratio: 25, hidden: true },
                { label: 'czh', name: 'czh', widthratio: 25, hidden: true },
                { label: 'yfdm', name: 'yfdm', widthratio: 25, hidden: true },
                { label: 'ypbzdm', name: 'ypbzdm', widthratio: 25, hidden: true },
                { label: 'fwfdj', name: 'fwfdj', widthratio: 25, hidden: true },
                { label: 'nbdl', name: 'nbdl', widthratio: 25, hidden: true },
                { label: 'ybdm', name: 'ybdm', widthratio: 25, hidden: true },
                { label: 'sfxmcbh', name: 'sfxmcbh', widthratio: 25, hidden: true }
            ],
            multiselect: true
        }, mydata);

        if (mydata.length > 0) {
            $('.ui-jqgrid-bdiv').find(".unwritten").remove();
        }
    }


    //as项目回车
    //ajax获取药品和项目信息
    function loadYpItemData() {
        //收费项目
        $("#sfxm").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            height: 260,
            width: 800,
            tableWidth: 1400,
            id: 'chargeItem',
            url: "/OutpatientManage/OutpatCharge/GetYPItemInfo?r=" + Math.random(),
            ajaxmethod: 'POST',
            ajaxreqdata: function () {
                var reqData = {};
                reqData.keyword = $.trim($("#sfxm").val());
                //sfxmkeyword = reqData.keyword;
                //if (sfxmkeyword == "" || sfxmkeyword.length < 2) {
                //    $.modalAlert("请输入精确查询条件,至少两位", 'warning');
                //    return false;
                //} else {
                return reqData;
                //}
            },
            caption: "收费项目",
            colModel: [
                { label: '项目与否', name: 'xmtype', width: 100, align: 'center', hiden: true },
                { label: '拼音', name: 'py', width: 110, align: "center" },
                { label: '通用名', name: 'xmmc', width: 200, align: "center" },
                { label: '商品名', name: 'spm', width: 200, align: "center" },
                { label: '单位', name: 'dw', width: 60, align: 'center' },
                {
                    label: '单价', name: 'dj', width: 60, align: 'center', formatter: function (cellvalue) {
                        return cellvalue ? cellvalue.toFixed(4) : "0.0000";
                    }
                },
                { label: '规格', name: 'ypgg', width: 80, align: 'center' },
                { label: '医保代码', name: 'ybdm', width: 100, align: 'center' },
                { label: '包装数', name: 'bzs', width: 60, align: 'center' },
                { label: '包装单位', name: 'bzdw', width: 100, align: 'center' },
                {
                    label: '剂量', name: 'jl', width: 60, align: 'center', formatter: function (cellvalue) {
                        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                    }
                },
                { label: '剂量单位', name: 'jldw', width: 80, align: 'center' },
                { label: '药厂名称', name: 'ycmc', width: 150, align: 'center' },
                { label: 'xmbh', name: 'xmbh', hidden: true },
                { label: 'sfxm', name: 'sfxm', hidden: true },
                { label: 'dl', name: 'dl', hidden: true },
                { label: 'dlmc', name: 'dlmc', hidden: true },
                {
                    label: 'zfbl', name: 'zfbl', hidden: true, formatter: function (cellvalue) {
                        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                    }
                },
                { label: 'zfxz', name: 'zfxz', hidden: true },
                { label: 'yfdm', name: 'yfdm', hidden: true },
                { label: 'xmtype', name: 'xmtype', hidden: true },
                { label: 'ypbzdm', name: 'ypbzdm', hidden: true },
                { label: 'nbdl', name: 'nbdl', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                var sfxmmc = $thistr.attr("data-xmmc");
                var dj = $thistr.attr("data-dj");
                $("#sfxm").val(sfxmmc);
                $("#dj").val(dj);
                $("#dw").val($thistr.attr("data-dw"));
                sfxmtype = $thistr.attr("data-xmtype");
                if (sfxmtype == "2") {
                    yfCode = $thistr.attr("data-yfdm");
                    ypbzdm = $thistr.attr("data-ypbzdm");
                    sfxmypgg = $thistr.attr("data-ypgg");
                }
                else {
                    yfCode = "";
                    ypbzdm = "";
                    sfxmypgg = "";
                }
                sfxmcbh = $thistr.attr("data-sfxm");
                sfxmbh = $thistr.attr("data-xmbh");
                sfxmzfbl = parseFloat($thistr.attr("data-zfbl"));
                sfxmdlmc = $thistr.attr("data-dlmc");
                sfxmdl = $thistr.attr("data-dl");
                sfxmzfxz = $thistr.attr("data-zfxz");
                sfxmNbdl = $thistr.attr("data-nbdl");
                //药品  同一处方不允许录入相同药品
                messCFXM = "";
                if (sfxmtype == "2") {
                    //判断是否已录入该药品或项目
                    messCFXM = cheYPEqual(sfxmcbh, yfCode);
                }
                if (sfxmkeyword != sfxmmc) { //说明从列表中选取了收费项目或药品
                    //检测同一处方是否有录入相同及作出相应处理
                    if (messCFXM == "") {
                        if (sfxmbh != "") {
                            lsCzh = "";
                            if (parseFloat(dj) == 0) {
                                $("#dj").attr("readonly", false);
                            }
                            else {
                                $("#dj").attr("readonly", true);
                            }
                        }
                        else {
                            $("#sfxm").focus();
                            $("#sfxm").val("");
                            return false;
                        }
                    } else {
                        if (messCFXM == "error") {
                            messCFXM = "不同药房药品不可在一张处方单上下达，是否换方？";
                            $.modalConfirm(messCFXM, function (flag) {
                                if (flag) {//换方
                                    GetNewCFH(); //获取新的处方号
                                    $("#sl").focus();
                                    if (parseFloat(obj.dj) == 0) {
                                        $("#dj").attr("readonly", false);
                                    }
                                    else {
                                        $("#dj").attr("readonly", true);
                                    }
                                }
                                else {//否
                                    clearHfItem();
                                }
                            });
                        } else {
                            $.modalConfirm(messCFXM, function (flag) {
                                if (flag) {
                                    lsCzh = "";
                                    clearHfItem();
                                    return false;
                                } else {
                                    lsCzh = sfxmcbh;
                                    $("#sl").focus();
                                    if (parseFloat(obj.dj) == 0) {
                                        $("#dj").attr("readonly", false);
                                    }
                                    else {
                                        $("#dj").attr("readonly", true);
                                    }
                                }
                            });
                        }
                    }
                }

            }
        });
    }

    //获取处方号或其他费用信息
    function loadCFHAndInfo() {
        //如果是药品需要处方号，项目不需要处方号
        //从后台获取服务费和处方号信息
        $.najax({
            type: "POST",
            url: "/OutpatientManage/OutpatCharge/GetCfAndSfInfo?r=" + Math.random(),
            data: { sfxmtype: sfxmtype, lscfh: lsCfh, brxz: brxz, dl: sfxmdl, sfxm: sfxmcbh, dj: $("#dj").val(), sl: $("#sl").val() },
            dataType: "json",
            success: function (ajaxresp) {
                cffwf = ajaxresp.data;
                lsCfh = cffwf.cfh; //临时处方号
                $("#lbldqcfh").text(lsCfh);
                sfxmfwf = cffwf.sfxmfwf;
                jeNum = cffwf.jeNum;
                //添加处方数据
                AddCFData();
            },
            errorCallback: function (err) {
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        })
    }

    //删除
    function DeleteGrid() {
        //获得选中行
        var gridIds = $("#listGrid").jqGrid('getGridParam', 'selarrrow');
        console.log(gridIds);
        if (gridIds.length > 0) {
            $.modalConfirm("确定要删除所选处方数据吗？", function (flag) {
                if (flag) {
                    //获取选中行的id
                    //遍历
                    for (var i = gridIds.length - 1; i >= 0; i--) {
                        $("#listGrid").jqGrid('delRowData', gridIds[i]);
                    }
                }
            });
        } else {
            $.modalAlert("暂无需要删除的处方信息！", 'warning');
        }
    }

    //模板
    function ItemPlate() {
        if (patInfoObj.patid != "" && patInfoObj.brxz !== "")
            LoadSfXMmb(); //加载收费项目模板
        else {
            $("#kh").focus();
            $.modalAlert("请确保病人有效！", 'warning');
            return false;
        }
    }

    //加载收费项目模版
    function LoadSfXMmb() {
        if ($("#mzgh").val() == "" || mzgh == "") {
            $("#mzgh").focus();
            $.modalAlert("请选择挂号！", 'warning');
            return false;
        }
        else if ($("#ys") == "" || ys == "") {
            $("#ys").focus();
            $.modalAlert("请选择医生！", 'error');
            return false;
        }
        else if ($("#ks").Text == "" || ks == "") {
            $("#mzgh").focus();
            $.modalAlert("请选择挂号！", 'warning');
            return false;
        }
        // 加载收费模版窗口，传入科室
        getSfxmMbAjax();
    }

    //后台获取收费模板
    function getSfxmMbAjax() {
        if (ys && ks) {
            $.modalOpen({
                id: "TemplateForm",
                title: "收费项目模板窗口",
                url: "/OutpatientManage/OutpatCharge/ItemTemplate?r=" + Math.random() + "&ks=" + ks,
                width: "700px",
                height: "530px",
                callBack: function (iframeId) {
                    var tempDetailData = top.frames[iframeId].submitForm();  //弹出页面的方法
                    if (tempDetailData) {
                        loadTempDetail(tempDetailData); //加载模板项目
                    }
                }
            });
        } else {
            $.modalAlert("请选择医生！", 'warning');
        }
    }

    //加载选中的模板项目
    function loadTempDetail(tempDetailData) {
        var tempData = [];
        var tempJe = 0;
        for (var i = 0; i < tempDetailData.length; i++) {
            var obj = new Object();
            obj.cfh = "";
            obj.xmmc = tempDetailData[i].sfxmmc; //项目
            obj.dj = tempDetailData[i].dj;
            obj.dw = tempDetailData[i].dw;
            obj.sl = tempDetailData[i].sl;
            obj.sfxm = tempDetailData[i].sfxm;
            obj.sfxmdlmc = tempDetailData[i].dlmc;
            obj.xmbh = tempDetailData[i].sfxmbh;
            obj.sfxmzfbl = parseFloat(tempDetailData[i].zfbl);
            obj.sfxmypgg = tempDetailData[i].gg;
            obj.dl = tempDetailData[i].dl;
            obj.zfxz = tempDetailData[i].zfxz;
            var sfxmfwf = 0;
            var je = ((parseFloat(obj.dj) + parseFloat(sfxmfwf)) * (parseFloat(obj.sl))).toFixed(2);
            //金额 = （单价 + 附加服务费）* 数量

            obj.fwfdj = sfxmfwf; //收费项目服务费
            obj.je = je;
            obj.brxzmc = $("#brxzmc").val();
            obj.ksmc = $("#ks").val();
            obj.ysmc = $("#ys").val();
            obj.xmnm = 0;
            obj.ghnm = ghnm;
            obj.mjzbz = mjzbz;
            obj.ksbh = ysksbh;
            obj.ysbh = ysbh;
            obj.ks = ks;
            obj.ys = ys;
            obj.xmtype = 4; //模板项目
            obj.pt_nm = 0;
            obj.pt_zdmxnm = 0;
            obj.czh = 0; //组号
            obj.yfdm = "";
            obj.ypbzdm = "";
            obj.nbdl = tempDetailData[i].nbdl;
            obj.ybdm = tempDetailData[i].ybdm;

            obj = $.extend(obj, patInfoObj);
            tempData.push(obj);
            if (tempJe != 0) {
                tempJe = parseFloat(tempJe) + parseFloat(obj.je);
            } else {
                tempJe = parseFloat(obj.je);
            }
            tempJe = tempJe.toFixed(2);

        }
        //两个数组合并
        myCFData.push.apply(myCFData, tempData);
        myCFCount++;
        $('#listGrid').jqGrid("clearGridData");//先清空数据
        gridListData(myCFData);

        $("#lbldqxmje").text(tempJe);//当前项目金额
        if (dqzje != 0) {
            dqzje = parseFloat(dqzje) + parseFloat(tempJe);
        } else {
            dqzje = parseFloat(tempJe);
        }
        dqzje = dqzje.toFixed(2);
        //$("#lblzje").text(dqzje); //总金额
        $("#txtysk").val(dqzje);

        checkedAll();

        clearItemXM(); //清空项目
    }


    //清除 F4
    function newtouch_event_f4() {
        funcDoPageClear();
    }


    //换方 F6
    function ChangeCF() {
        if (myCFData.length > 0) {
            //判断数据是否已变更 stateSave=false
            clearHfItem();
        } else {
            $("#kh").focus();
            return false;
        }
    }

    //换方清空
    function clearHfItem() {
        clearItemXM(); //清空项目，不清除挂号和医生、科室
        GetNewCFH(); //获取新的处方号
    }


    //添加一个处方后清空之前项目数据
    function clearItem() {
        $("#sfxm").val("");
        $("#sl").val("");
        $("#dw").val("");
        $("#dj").val("");
        $("#ys").val("");
        $("#ks").val("");
        $("#mzgh").val("");
        $("#sfxm").focus();
    }

    function clearItemXM() {
        $("#sfxm").val("");
        $("#sl").val("");
        $("#dw").val("");
        $("#dj").val("");
        $("#sfxm").focus();
        $("#mzgh,#ys,#ks").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
    }

    //换方获取新的处方号
    function GetNewCFH() {
        $.loading(true);
        $.najax({
            type: "POST",
            url: "/OutpatientManage/OutpatCharge/GetCfh?r=" + Math.random(),
            data: { sfxmtype: sfxmtype },
            dataType: "json",
            success: function (ajaxresp) {
                lsCfh = ajaxresp.data; //临时处方号
                $("#lbldqcfh").text(lsCfh);
            },
            errorCallback: function (err) {
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        });

    }

    var ysk = 0, //应收款
        zfje = 0, //支付金额
        zl = 0, //找零
        xjzffs, //现金支付方式
        xjzffsbh, //编号
        zffsmc,
        gridIds, //选中行id
        fph; //发票号

    //保存 判断
    function CheckSave() {
        if (kh === "" || patInfoObj == null || patInfoObj.patid == "" || patInfoObj.brxz === "") {
            $.modalAlert("病人基本信息为空", 'warning');
            return false;
        }
        if (fph == "") {
            $.modalAlert("请选择发票号", 'warning');
            return false;
        }

        if (gridIds == "" || gridIds == undefined) {
            $.modalAlert("没有需要保存和结算的数据", 'warning');
            return false;
        }

        if (parseFloat(ysk) == 0) {
            grid_checkedit();
        }

        var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        zfje = $("#txtmoney").val();  //支付金额
        var ddlzffs = $("#ddlzffs option:selected");//获取选中的项
        xjzffs = ddlzffs.val();//拿到选中项的值
        zffsmc = ddlzffs.text();//拿到选中项的文本
        xjzffsbh = ddlzffs.attr("data-xjzffsbh"); //获取data-*自定义属性

        fph = $("#txtfph").val();
        if (xjzffs == -1 || zffsmc == "") {
            $.modalAlert("支付方式必选", 'warning');
            return false;
        }
        if (zfje == "" || zfje == undefined) {
            $.modalAlert("支付金额必填", 'warning');
            return false;
        } else {
            if (!reg.test(zfje)) {
                $.modalAlert("请输入正确的金额格式", 'warning');
                return false;
            }
        }
        if (parseFloat(zfje) < parseFloat(ysk)) {
            $.modalAlert("实付金额必须不能小于应支付金额", 'warning');
            return false;
        } else {
            zl = parseFloat(zfje) - parseFloat(ysk);
            zl = zl.toFixed(2);
            $("#txtzl").val(zl);
            //结算窗体
            showBillSuccessDialog(ysk, zfje, 0, zl);
        }
    }

    //弹出金额确认窗口
    function showBillSuccessDialog(ysk, ssk, srce, zl) {
        var url = "/HospitalizationManage/Settlement/SettSuccessDialog?r=" + Math.random() + "&yingshoukuan=" + ysk
            + "&ssk=" + ssk
            + "&srce=" + srce
            + "&zhaoling=" + zl;
        $.modalOpen({
            id: "FormShowBillSuccessDialog",
            title: "结算确认",
            url: url,
            width: "320px",
            height: "260px",
            callBack: function (iframeId) {
                $.modalClose("FormShowBillSuccessDialog");   //关闭结算金额的窗体
                var allJson = $("#listGrid").jqGrid('getRowData_AllLine', true); //获取选中行的Json数据
                SaveAjax(JSON.parse(allJson));
            }
        });
    }

    //保存 F8
    function newtouch_event_f8() {
        //先判断是否选择处方、支付方式、金额
        CheckSave();
        //弹出框
    }

    //保存 ajax 后台添加处方和项目、结算
    function SaveAjax(allJson) {
        //把处方数据传到后台
        $.najax({
            type: "POST",
            url: "/OutpatientManage/OutpatCharge/AddCfInfo",
            data: { chargeDto: allJson, kh: kh, fph: fph, xjzffs: xjzffs, xjzffsbh: xjzffsbh, zfje: zfje, ysk: ysk, zl: zl },
            dataType: "json",
            cache: false, //修复ie缓存
            success: function (ajaxresp) {
                $.modalAlert("保存成功", 'success');
                newtouch_globalevent_f4();//清除
            },
            errorCallback: function (err) {
                newtouch_globalevent_f4();//清除
                $.loading(false);
            },
            complete: function () {
                lsCfh = "";
                $.loading(false);
            }
        });
    }


    //jqGrid多选框 单选后改变应收款金额
    function grid_checkedit() {
        ysk = 0;
        gridIds = $("#listGrid").jqGrid('getGridParam', 'selarrrow'); //获取选中行的id
        //同样可以
        //var zzzzzfje = 0;
        //var allJson = $("#listGrid").jqGrid('getRowData_AllLine', true); //获取选中行的Json数据
        //var allArr = JSON.parse(allJson);
        //for (var i = 0; i < allArr.length; i++) {
        //    var skje = allArr[i].je;
        //    console.log(skje);
        //    zzzzzfje += parseFloat(skje);
        //}
        //console.log(zzzzzfje);

        if (gridIds != "" && gridIds != undefined) {
            for (var i = 0; i < gridIds.length; i++) {
                var rowData = $("#listGrid").jqGrid("getRowData", gridIds[i]);//根据上面的id获得本行的所有数据
                var xmje = rowData.je; //获得指定列的值 （auditStatus 为colModel的name）
                ysk = parseFloat(ysk) + parseFloat(xmje);
            }
        }
        ysk = ysk.toFixed(2);
        $("#txtysk").val(ysk); //应收款随着选择改变

        //当前选中行的cfh 如果不为空是药品，如果为空是收费项目
        var gr = $("#listGrid").jqGrid('getGridParam', 'selrow');//当前点击行
        var dqcfobj = $("#listGrid").jqGrid("getRowData", gr);
        var dqcfje = 0;
        if (dqcfobj != "") {
            //药品
            for (var i = 0; i < myCFData.length; i++) {
                if (myCFData[i].cfh == dqcfobj.cfh) {
                    if (dqcfje != 0) {
                        dqcfje = parseFloat(dqcfje) + parseFloat(myCFData[i].je);
                    } else {
                        dqcfje = parseFloat(myCFData[i].je);
                    }
                }
            }

        } else {//项目
            dqcfje = $("#listGrid").jqGridRowValue()[0].je;
        }

        dqcfje = dqcfje.toFixed(2);
        $("#lbldqxmje").text(dqcfje); //当前处方金额
    }


    //默认全选
    function checkedAll() {
        $("#tabGrid input[type=checkbox]").attr("checked", true);
        $("#tabGrid tbody input[type=checkbox]").each(function (i) {
            $("#listGrid").jqGrid('setSelection', i + 1); //设置jqGrid选中
        });
        //全选框
        var $checkAll = $("#tabGrid table thead").find("input");
        //body 各行的checkbox框
        var $tbr = $("#tabGrid table tbody").find("input");
        $checkAll.prop('checked', $tbr.find('input:checked').length == $tbr.length ? true : false);
    }


</script>
